<?php

	/**
	 * 日期转换
	 *
	 * @param string $time 转换前时间
	 * @param string $mode 转换模式
	 * @return string 返回转换后的时间
	 */
	function toDate($time,$mode="m-d H:i"){
		$now = time();
		$flag = (int)($now - $time);
		if($flag<60){
			return $flag.L("secondlast");
		}else if($flag<3600){
			return (int)($flag / 60) . L("minlast");
		}else if ($flag < 3600 * 24){
			return  (int)($flag / 3600) . L("hourlast");
		}else if ($flag < 3600 * 24 *2){
			return L("yesterday");
		}else if ($flag < 3600 * 24*3){
			return L("beforyesterday");
		}else{
			return date($mode,$time);
		}
	}
	/**
	 *  格式化日期
	 *
	 * @param int $time 要格式的时间
	 * @return date 返回格式化后的日期
	 */
	function toYmd($time){
		if($time==0){
			return L("now");
		}else{
			return date("Y-m-d",$time);
		}
	}
	
	/**
	 *  REQUEST相关参数过滤
	 *
	 * @param string $str 要过滤参数
	 * @param int $type   参数类型  0表数字型 1表字符串
	 * @return $str 返回安全的参数
	 */
	function safeRequest($str,$type=0){
		if((empty($str)))
			return "all";
		if ($type==0){
			if (is_numeric($str)){
				return $str;
			}else{
				redirect(__APP__."/rank/nosafe");
			 }
		}else{
			if (eregi("\'|\"|and|or |select|insert into|update|delete|union",$str)){  
				redirect(__APP__."/rank/nosafe");
				exit();
			}else{
				return $str;
			}
		}
	}
	
	/**
	 *格式化字符串中的html
	 *
	 * @param string $str
	 * @return string  返回格式化的字符串，
	 */
	function str_filter($str){
		return htmlspecialchars($str,ENT_QUOTES);
	}
	
	/**
	 * 获得cache层级
	 *
	 * @param int $id
	 * @return 返回层级目录
	 */
	function getDeep($id){
		$deep1 = (int)($id / 1000);//每个文件夹最多存放1000个目录
		$deep2 = $deep1/1000;
		return $deep1."/".$deep2."/".$id;
	}
	
	/**
	 * 根据userid获得用户cache地址
	 *
	 * @param int $userid 用户ID
	 * @return 返回cache地址
	 */
	function getCachePath($rootDir,$userid){
		return $rootDir."/".getDeep($userid)."/";
	}
	
	/**
	 * 初始化用户相关cache目录
	 *
	 * @param int $userid 用户ID
	 */
	function makeUserCache($userid){
		
		//创建相关目录
		$path = getCachePath(APP_PATH."/UserCache",$userid);
		mk_dir($path);
		
		//用户头像目录
		mk_dir("..".WEB_PUBLIC_URL."/Uploads/face/".getDeep($userid)); 
		mk_dir("..".WEB_PUBLIC_URL."/Uploads/face/big/".getDeep($userid)); 
		mk_dir("..".WEB_PUBLIC_URL."/Uploads/face/small/".getDeep($userid));
		
		//初始化cache表
		$userCacheDao = D("cache");
		$userCacheDao->userid = $userid;
		$userCacheDao->add(); 
		F("userData",$userCacheDao->data,-1,$path);
		
		//初始化setting表
		$userSettingDao = D("user_setting");
		$userSettingDao->userid = $userid;
		$userSettingDao->add();
		F("userSetting",$userSettingDao->data,-1,$path);
	}
	
	/**
	 * 创建相册目录
	 *
	 * @param int $userid  用户ID
	 * @param int $albumid 相册ID
	 * @return null
	 */
	function makeAlbum($userid,$albumid){
		mk_dir("./Public/Uploads/photo/".getDeep($albumid));
		mk_dir("./Public/Uploads/photo/big/".getDeep($albumid));
		mk_dir("./Public/Uploads/photo/small/".getDeep($albumid));
	}
	
	/**
	 * 添加动态
	 *
	 * @param int $userid  用户ID
	 * @param string $module  模块名
	 * @param string $content  内容
	 * @param int $moduleid  模块id
	 * @param int $requestid  请求者ID
	 * @param int $count  相同模块操作汇总
	 * @return feedid
	 */
	function addFeed($userid,$module,$content,$moduleid=0,$requestid=0,$count=1){
		$feedDao = D("feed");
		$feedDao->userid = $userid;
		$feedDao->module = $module;
		$feedDao->moduleid = $moduleid;
		$feedDao->regtime = time();
		$feedDao->requestid = $requestid;
		$coalitionModule = F("coalitionModule");
		if(in_array($module,$coalitionModule)){
			//合并一天内的相同动态
			$record = $feedDao->find("userid = ".$userid." and module='".$module."' and moduleid=".$moduleid." and regtime > ($feedDao->regtime - 86400)");
			if($record){
				$_content = $feedDao->content.",".$content;
				$feedDao->content = $_content;
				$feedDao->count = '(count+'.$count.')';
				$feedDao->regtime = time();
				return $feedDao->save(); 
			}else{
				return $feedDao->add();
			}
		}else{
			return  $feedDao->add();
		}
	}
	/**
	 * 删除动态
	 *
	 * @param int $id  动态ID
	 * @param int $userid  用户ID
	 * @param string $module  模块名
	 * @param string $content  内容
	 * @param int $moduleid  模块id
	 * @param int $requestid  请求者ID
	 * @return true
	 */
	function delFeed($id,$userid,$module="feed",$moduleid=0,$requestid=0){
		$feedDao = D("feed");
		if($moduleid == 0 ){
			$feedDao->delete($id);
		}else {
			$where = "";
			if($moduleid != 0){
				$where .= " and moduleid=".$moduleid;
			}
			if($requestid != 0){
				$where .= " and requestid=".$requestid;
			}
			$feedDao->delete("module='".$module."'".$where);
		}
	}
	

	/**
	 * 判断我与他的级别关系 0：自己 1:好友 2：同网络 3: 同站
	 *
	 * @param int $id  我的ID
	 * @param int $userid  好友的ID
	 * @return int
	 */
	function getSetting($myid,$userid){
		if($myid == $userid){ //是自己
			return "0";
		}elseif(isFriend($userid,$myid)=="yes"){ //好友
			return "1";
		}else{
			$path =  getCachePath(APP_PATH."/UserCache",$userid);
			$mypath = getCachePath(APP_PATH."/UserCache",$myid);
			$mynetwork = F("userInfo","",-1,$mypath);
			$usernetwork= F("userInfo","",-1,$path); //用户的网络					 				
			if($usernetwork['network_city'] == $mynetwork['network_city']){//是否同一网络
				return "2";
			}else{
				return "3";
			}
		}
	}
	
	/**
	 * 判断阅读模块的权限 
	 *
	 * @param int $myid  我的ID
	 * @param int $userid  好友的ID
	 * @param string $module  模块名
	 * @param int $setting  设置权限
	 * @return int
	 */
	function getRank($myid,$userid,$module="home",$setting=2){
		$path =  getCachePath(APP_PATH."/UserCache",$userid);
		if($myid == $userid){ //查看人是自己
			return true;
		}else{
			if($module=="other"){
				$user_setting = $setting;
			}else{
				$userSetting = F("userSetting",'',-1,$path);
				$user_setting = $userSetting[$module];
			}
			if("0" == $user_setting){//仅自己
				return false;
			}else{
				if("yes" == isFriend($userid,$myid)){ //是否好友
					return true;
				}else{
					if(3 == $user_setting){//全站可见
						return true;
					}elseif(2 == $user_setting){//同网络可见
						$userNetWork = F("userInfo","",-1,$path); //用户的网络
						$myNetWork = F("userInfo","",-1,$path);  //我的网络	
						if($myNetWork['network_city'] == $userNetWork['network_city']){//是否同一网络
							return true;
						}else{
							return false;
						}
					}
				}
			}
		}
	}
	
	/**
	 * 好友情况判断 
	 *
	 * @param int $myid  我的ID
	 * @param int $userid  好友的ID
	 * @return int
	 */
	function isFriend($userid,$myid){
		$path =  getCachePath(APP_PATH."/UserCache",$myid);
		$friends = F("userFriends","",-1,$path);
		if(in_array($userid,$friends)){ //在好友列表中
			return "yes";
		}else{
			$requestDao = D("friends_requests");
			if($requestDao->find("userid={$userid} and requestid={$myid}",'id')){ //在请求列表中
				return $request['id']; 
			}else{ ////不是好友
				return "no"; 
			}
		}
	}
	
	/**
	 * 取得好友的id集合，并用,隔开 
	 *
	 * @param int $userid  用户ID
	 * @return string
	 */
	function getFriendList($userid){
		$path =  getCachePath(APP_PATH."/UserCache",$userid);
		$friends = F("userFriends","",-1,$path);
		$friendids = implode(",",$friendid);
		return $friendids;
	}
	
	/**
	 * 获取某个模块的动态设置，来确定相关动作是否反应到个人动态中 
	 *
	 * @param int $userid  用户ID
	 * @param string $module 模块
	 * @param int $type 返回值的形式
	 * @param int $setting 自定义的设置
	 * @return int/true/false
	 */
	function getAddFeedSetting($userid,$module = "infoaction",$type=0,$setting=""){
		if("" == $setting){
			$path =  getCachePath(APP_PATH."/UserCache",$userid);
			$$userSetting = F("userSetting","",-1,$path);
			if(0==$type){
				if(1 == $userSetting[$module] ){
					return true;
				}else{
					return false;
				}
			}else{
				return $userSetting->$module;
			}
		}elseif(1 == $setting){
			return true;
		}elseif(0 == $setting){
			return false;
		}
	}
	
	/**
	 * 添加通知
	 *
	 * @param int $userid  用户ID
	 * @param int $myid   我的ID
	 * @param string $module 模块
	 * @param int $moduleid 模块ID
	 * @param int $revertid 响应者ID
	 * @parame string $content 通知内容
	 * @return int/true/false
	 */
	function addNotify($userid,$myid,$module,$moduleid,$revertid=0){
		$dao = D("notify");
		$dao->userid    = $userid;
		$dao->postid    = $myid;
		$dao->module 	= $module;
		$dao->moduleid	= $moduleid;
		$dao->revertid	= $revertid;
		$dao->regtime 	= time();
		return $dao->add();
	}
	
	/**
	 * 获取通知
	 *
	 * @param int $id  通知ID
	 * @param int $postid  用户ID
	 * @param int $postname   我的ID
	 * @param string $module 模块
	 * @param int $moduleid 模块ID
	 * @param int $revertid 响应者ID
	 * @return string
	 */
	function getNotify($id,$postid,$postname,$module,$moduleid,$revertid){
		D("notify")->setField("isread",1,"id=".$id);
		$content = F("notify_".$module);
		return "<a href=\"".__APP__."/home/".$postid."\">".$postname."</a>".str_replace(array("#moduleid#", "#revertid#"),array($moduleid,$revertid),$content); 
	}
	
	/**
	 * 删除通知
	 *
	 * @param int $id  通知ID
	 * @param string $module 模块
	 * @param int $moduleid 模块ID
	 * @return true/false
	 */
	function delNotify($id,$module,$moduleid){
		$dao = D("notify");
		if($id==0){
			if(is_array($module)){
				$where = "";
				foreach($module as $item){
					$where .= "(module='".$item."') or ";
				}
				$where .= "1=0";
				return $dao->delete($where."' and moduleid=".$moduleid);
			}else{
				return $dao->delete("module='".$module."' and moduleid=".$moduleid);
			}
		}else{
			return $dao->delete("id=".$id);	
		}
	}
	
	
	/**
	 * 根据ID获得某个字段的值
	 *
	 * @param int $id  ID
	 * @param string $module 模块
	 * @param int $field 字段
	 * @return string
	 */
	function getField($model,$field,$id){
		$dao = D($model);
		$record = $dao->find('id='.$id,$field);
		return $record[$field];
	}
	
	/**
	 * 获得用户的最新状态 
	 *
	 * @param int $userid  用户ID
	 * @return string
	 */
	function getLastMini($userid){
		$record = D("mini")->find("userid=".$userid,"regtime,content","regtime desc",1);
		if($record){
			return $record->content.'<span class="li_time">'.toDate($record->regtime).'</span>';
		}else{
			return "<span>我是个沉默的大多数</span>";
		}
	}


	
	
	/**
	 * 根把条件获得记录数
	 *
	 * @param int $id  ID
	 * @param string $module 模块
	 * @param int $field 字段
	 * @return int $count
	 */
	function getRecordCountByid($model,$field,$id){
		$dao = D($model);
		$count = $dao->Count("$field=$id","id");
		return $count;
	}
	
	/**
	 * 根据ID获得用户名
	 *
	 * @param int $userid  ID
	 * @return string
	 */
	function getUsernameByUserid($userid){
		if(intval($userid)>0){
			$path =  getCachePath(APP_PATH."/UserCache",$userid);
			$record = F("userInfo","",-1,$path);
			return $record['username'];
		}else{
			return null;
		}
	}
	
	/**
	 * 根据ID获得用户名及网络
	 *
	 * @param int $userid  用户ID
	 * @return string 
	 */
	function getUNByUserid($userid){
		$path =  getCachePath(APP_PATH."/UserCache",$userid);
		$record = F("userInfo","",-1,$path);
		return $record['username']."(".$record['network_city'].")";
	}
	
	
	/**
	 * 动态显示
	 *
	 * @param 
	 * @return string 
	 */
	function getModule($userid,$module,$moduleid,$content,$myid,$requestid,$count,$regtime){
		$tmp = F("feed_".$module,"",-1,APP_PATH."/SiteCache/");
		eval($tmp); //很危险的eval
		return $text;
	}
	
	
	/**
	 * 分享显示
	 *
	 * @param 
	 * @return string 
	 */
	function getShare($module,$moduleid,$shareurl,$type=0,$content="",$userid=0,$myid=0,$regtime){
		$tmp = F("feed_".$module,"",-1,APP_PATH."/SiteCache/");
		eval($tmp);
		return $text;
	}
?>